package com.hry.store.cms.controller;

import com.github.pagehelper.PageInfo;
import com.hry.store.cms.common.Const;
import com.hry.store.cms.common.ServerResponse;
import com.hry.store.cms.pojo.TopCategory;
import com.hry.store.cms.service.ProductVoService;
import com.hry.store.cms.service.TopCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private ProductVoService productVoService;
    @Autowired
    private TopCategoryService topCategoryService;

    /**
     * 带有分页查询后台所有商品
     */

    @RequestMapping("product/getAll")
    public String getAllProduct(ModelMap modelMap, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo pageInfo = productVoService.getAll(pageNum, pageSize);
        List<TopCategory> topCategoryList = topCategoryService.getAll();
        modelMap.put("topCategoryList", topCategoryList);
        modelMap.put("pageInfo", pageInfo);
        return "product";
    }

    /**
     * 条件查询
     */
    @RequestMapping("product/withSelected")
    public String withSelected(ModelMap modelMap, @RequestParam("productStatus") Integer status,
                               @RequestParam("beginTime") String beginTime,
                               @RequestParam("endTime") String endTime,
                               @RequestParam("productCategory") Integer topCategoryId,
                               @RequestParam("keyWords") String keyWords,
                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo pageInfo = productVoService.withSelected(pageNum, pageSize, status, beginTime, endTime, topCategoryId, keyWords);
        modelMap.put("pageInfo", pageInfo);
        List<TopCategory> topCategoryList = topCategoryService.getAll();
        modelMap.put("topCategoryList", topCategoryList);
        return "product";
    }

    /**
     * 更新商品
     */
    @RequestMapping("product/updateProduct")
    @ResponseBody
    public ServerResponse updateProduct(@RequestParam("secondCategoryId") Integer secondCategoryId,

                                        @RequestParam("editProductQuantity") Integer editProductQuantity,

                                        @RequestParam("editLunBoStatus") Integer editLunBoStatus,
                                        @RequestParam("editHotStatus") Integer editHotStatus,
                                        @RequestParam("editSellStatus") Integer editSellStatus,
                                        @RequestParam("productDescs") String productDesc,
                                        @RequestParam("colorCode") String colorCode) {
        int result = productVoService.updateProduct(secondCategoryId,  editProductQuantity, productDesc, editLunBoStatus, editHotStatus, editSellStatus, colorCode);
        if (result > 0) {
            return ServerResponse.createBySuccess("更新成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "更新失败");
    }

    /**
     * 删除商品
     */
    @RequestMapping("product/deleteProduct")
    @ResponseBody
    public ServerResponse deleteProduct(@RequestParam("secondCategoryId") Integer secondCategoryId) {
        boolean result = productVoService.deleteByCategoryId(secondCategoryId);
        if (result) {
            return ServerResponse.createBySuccess("删除成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "删除失败");
    }




    /**
     *添加热门商品名称
     */


}
